package com.my.mycommondomain.domian;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class R <T>{
    private String msg;
    private int code;
    private T data;

    public static <T> R<T> ok(){
        return restResult(null, ResultCode.SUCCESS.getMsg(), ResultCode.SUCCESS.getCode());
    }
    public static <T> R<T> ok(T data){
        return restResult(data,ResultCode.SUCCESS.getMsg(), ResultCode.SUCCESS.getCode());
    }
    public static <T> R<T> ok(T data,String msg){
        return restResult(data,msg,ResultCode.SUCCESS.getCode());
    }
    public static <T> R<T> fail(){
        return restResult(null,ResultCode.FAILED.getMsg(), ResultCode.FAILED.getCode());
    }
    public static <T> R<T> fail(String msg){
        return restResult(null,msg,ResultCode.FAILED.getCode());
    }
    public static <T> R<T> fail(T data){
        return restResult(data,ResultCode.FAILED.getMsg(),ResultCode.FAILED.getCode());
    }
    public static <T> R<T> fail(Integer code,String msg) {
        return restResult(null, msg, code);
    }
    public static <T> R<T> fail(T data,String msg){
        return restResult(data,msg,ResultCode.FAILED.getCode());
    }

    private static <T> R<T> restResult(T data, String msg, int code){
        R<T> apiResult = new R<T>();
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setCode(code);
        return apiResult;
    }
}
